package runtimeAnnotations;

import java.lang.reflect.*;

public class ToStrings
{
   public static String toString(Object obj)
   {
      if (obj == null) return "null";
      Class<?> cl = obj.getClass();
      ToString ts = cl.getAnnotation(ToString.class);
      if (ts == null) return obj.toString();
      var result = new StringBuilder();
      if (ts.includeName()) result.append(cl.getName());
      result.append("[");
      boolean first = true;
      for (Field f : cl.getDeclaredFields())
      {
         ts = f.getAnnotation(ToString.class);
         if (ts != null) {
            if (first) first = false; else result.append(",");
            f.setAccessible(true);
            if (ts.includeName())
            {
               result.append(f.getName());
               result.append("=");
            }
            try
            {
               result.append(ToStrings.toString(f.get(obj)));
            }
            catch (ReflectiveOperationException ex)
            {
               ex.printStackTrace();
            }
         }
      }
      result.append("]");
      return result.toString();
   }
}